#!/usr/bin/env python
# -*- coding: utf-8 -*-

import tornado
import datetime
import urllib
import etc
import conf
import data_user
import data_home
import data_topic
import log
import conf
from session_base import Session
from session_mc import BaseHandler


class HomeHandler( BaseHandler ) :

    @tornado.web.asynchronous
    def get( self ) :
        next = self.get_argument( 'next', default=conf.url['host_home'] )
        self.get_current_user()
        isAdmin = False
        if self.ss_data :
            uid = self.ss_data['uid']
            if uid == conf.admin_uid :
                isAdmin = True
        self.render( "host_home.html",
                p_msg = conf.msg,
                p_url = conf.url,
                p_session = self.ss_data,
                p_pwsecret = etc.pw_secret,
                p_email = None,
                p_passwd = None,
                p_isAdmin = isAdmin,
                p_next = urllib.quote(next),
            )


class ProfileHandler( BaseHandler ) :

    @Session
    def get( self ) :
        uid = self.ss_data['uid']
        user = data_user.get_user_by_id( conf.db.user, uid )
        email = user['email']
        self.render( "host_profile.html",
                p_msg = conf.msg,
                p_url = conf.url,
                p_session = self.ss_data,
                p_pwsecret = etc.pw_secret,
                p_email=email,
                p_phone = phone,
            )


class HomeListAdminHandler( BaseHandler ) :

    @Session
    def get( self ) :
        self.render( "host_home_list_admin.html",
                p_msg = conf.msg,
                p_url = conf.url,
                p_session = self.ss_data,
            )


class HomeListInfoHandler( BaseHandler ) :

    @Session
    def post( self ) :
        uid = self.ss_data['uid']
        isAdmin = False
        if uid == conf.admin_uid :
            isAdmin = True
        appstats = data_home.find_appstats( uid )
        appstats_dict = {}
        for i in appstats :
            appstats_dict[i['appkey']] = i
        self.render( "host_home_list_info.html",
                p_msg = conf.msg,
                p_url = conf.url,
                p_isAdmin = isAdmin,
                p_initAppkey = conf.init_appkey,
                p_session = self.ss_data,
                p_appstats_dict = appstats_dict,
            )


class HomeListStatHandler( BaseHandler ) :

    @Session
    def get( self ) :
        listnode = None
        urlitems = None
        if self.get_current_user()  :
            uid = self.ss_data['uid']
            list = self.ss_data['listinfo']['list']
            appid = self.get_argument( 'appid',default=None )
            if appid :
                for i in list :
                    if i['appid'] == long(appid) :
                        listnode = i
                        urlitems = data_home.get_items( listnode['links'] )
                        break
        #log.i( listnode )
        #log.i( urlitems )
        if not listnode :
            return self.redirect( conf.url['host_home'] )
        st_all = data_home.get_st_all_by_appid( appid )
        date=datetime.datetime.now()
        self.render( "host_home_list_stat.html",
                p_msg = conf.msg,
                p_url = conf.url,
                p_session = self.ss_data,
                p_listnode = listnode,
                p_urlitems = urlitems,
                p_st_all = st_all,
                p_date = date,
            )


class HomeMainAdminHandler( BaseHandler ) :

    @Session
    def get( self ) :
        uid = self.ss_data['uid']
        list = self.ss_data['listinfo']['list']
        appid = self.get_argument( 'appid',default=None )
        if not appid :
            return self.redirect( conf.url['host_home'] )
        listnode = None
        for i in list :
            if i['appid'] == long(appid) :
                listnode = i
                break
        if not listnode :
            return self.redirect( conf.url['host_home'] )
        self.render( "host_home_main_admin.html",
                p_msg = conf.msg,
                p_url = conf.url,
                p_session = self.ss_data,
                p_listnode = listnode,
            )


class HomeMainInfoHandler( BaseHandler ) :

    @Session
    def post( self ) :
        listnode = None
        urlitems = None
        uid = self.ss_data['uid']
        list = self.ss_data['listinfo']['list']
        appid = self.get_argument( 'appid',default=None )
        if appid :
            for i in list :
                if i['appid'] == long(appid) :
                    listnode = i
                    urlitems = data_home.get_items( listnode['links'] )
                    break
        topic_info = data_topic.get_topic_info( )
        #log.i( listnode )
        #log.i( urlitems )
        self.render( "host_home_main_info.html",
                p_msg = conf.msg,
                p_url = conf.url,
                p_session = self.ss_data,
                p_listnode = listnode,
                p_urlitems = urlitems,
                p_topic_info = topic_info,
            )


class TopicAdminHandler( BaseHandler ) :

    @Session
    def get( self ) :
        uid = self.ss_data['uid']
        if uid != conf.admin_uid :
            return self.redirect( conf.url['host_home'] )
        self.render( "host_topic_admin.html",
                p_msg = conf.msg,
                p_url = conf.url,
                p_session = self.ss_data,
            )


class TopicInfoHandler( BaseHandler ) :

    @Session
    def post( self ) :
        if not self.get_current_user()  :
            return self.redirect( conf.url['host_home'] )
        uid = self.ss_data['uid']
        if uid != conf.admin_uid :
            return self.redirect( conf.url['host_home'] )
        topics = []
        begin = self.get_argument( 'begin', default=0 )
        limit = self.get_argument( 'limit', default=10 )
        topics = data_topic.get_topics( begin, limit )
        #log.i( 'topic from : %s limit : %s res : %s'%(begin,limit,topics) )
        self.render( "host_topic_info.html",
                p_msg = conf.msg,
                p_url = conf.url,
                p_session = self.ss_data,
                p_topics = topics,
            )


class TopicStatHandler( BaseHandler ) :

    @Session
    def get( self ) :
        uid = self.ss_data['uid']
        if uid != conf.admin_uid :
            return self.redirect( conf.url['host_home'] )
        topics = []
        begin = self.get_argument( 'begin', default=0 )
        limit = self.get_argument( 'limit', default=10 )
        topics = data_topic.get_topics( begin, limit )
        st_all = data_topic.get_st_all()
        date=datetime.datetime.now()
        self.render( "host_topic_stat.html",
                p_msg = conf.msg,
                p_url = conf.url,
                p_session = self.ss_data,
                p_topics = topics,
                p_st_all = st_all,
                p_date = date,
            )


